package main;

import java.util.ArrayList;

public class BitString {

	static ArrayList<Integer[]> masterBitStringList = new ArrayList<Integer[]>();

	static void uniqueAdd(Integer[] array){
		
		for(Integer[] bitString : masterBitStringList){
			for (int i=0;i<array.length;i++){
				if(array[i] != bitString[i]){
					break;
				}
				if(i == array.length - 1){
					return;
				}
			}
		}
		
		masterBitStringList.add(array);
	}
		
	public static void main(String[] args) {

		//Trigger dependecies
		ArrayList<Integer[]>triggerDeps = new ArrayList<Integer[]>();
		
		Integer[] temp = {1,1,1};
		Integer[] temp1 = {1,1,0};
		Integer[] temp2 = {1,0,1};
		
		triggerDeps.add(temp);
		triggerDeps.add(temp1);
		triggerDeps.add(temp2);
		
		for (Integer[] bitString : triggerDeps){
			uniqueAdd(bitString);
		}

		//Example reaction dependencies
		ArrayList<Integer[]>reactionDeps = new ArrayList<Integer[]>();
		
		Integer[] tmp = {1,0,0};
		Integer[] tmp1 = {0,1,1};
		Integer[] tmp2 = {0,0,1};
		Integer[] tmp3 = {0,0,1};
		Integer[] tmp4 = {0,0,1};
		
		reactionDeps.add(tmp);
		reactionDeps.add(tmp1);
		reactionDeps.add(tmp2);
		reactionDeps.add(tmp3);
		reactionDeps.add(tmp4);
		
		for (Integer[] bitString : reactionDeps){
			uniqueAdd(bitString);
		}
		
		Integer[] bitString;
		for (int k=0;k<masterBitStringList.size();k++){
			bitString = masterBitStringList.get(k);
			
			System.out.print("f");
			for(int i=0;i<bitString.length;i++){
				System.out.print(bitString[i]);				
			}
			System.out.println("(){");
			
			for(int i=0;i<bitString.length;i++){
				if (bitString[i] == 1){
					System.out.println("\tif (trig[" + i + "].condition()){");
					System.out.println("\t\ttrig[" + i + "].action();");					
					
					Integer copy[] = new Integer[bitString.length];					
					for(int j=0;j<bitString.length;j++){
						copy[j] = triggerDeps.get(i)[j];
					}
					
					for(int j=i+1;j<bitString.length;j++){
						copy[j] |= bitString[j];
					}
					
					System.out.print("\t\tf");
					for(int j=0;j<copy.length;j++){
						System.out.print(copy[j]);				
					}
					System.out.println("();");
					
					uniqueAdd(copy);
					
					System.out.println("\t\treturn;");
					
					System.out.println("\t}");
				}
			}
			System.out.println("}\n");
		}
	}
}
